package cate.game.role.pay.trademall.oncefund;

import cate.common.table.trademall.oncefund.OnceFundCommonRow;
import cate.common.table.trademall.oncefund.OnceFundItemRow;
import cate.common.util.GameResult;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.pay.trademall.oncefund.msg.OnceFundInfoResp;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.Map;

@NoteClass(value = "一次性基金")
public class OnceFundAllPO extends RoleDependent {
    @NoteField(value = "一次性基金组")
    public Map<Integer, OnceFundPO> fundMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (fundMap == null) {
            fundMap = new HashMap<>();
        }
        fundMap.entrySet().removeIf(entry -> role.getGame().table.tradeMall.once_fund_common.get(entry.getKey()) == null);
        fundMap.entrySet().removeIf(entry -> role.getGame().table.tradeMall.once_fund_common.get(entry.getKey()).fundGroup == 0);
        for (OnceFundCommonRow configRow : role.getGame().table.tradeMall.once_fund_common.getList()) {
            if (fundMap.containsKey(configRow.id)) {
                continue;
            }
            OnceFundPO po = new OnceFundPO(configRow.id, configRow.fundType, configRow.fundGroup);
            fundMap.put(configRow.id, po);
        }
        fundMap.values().forEach(po -> po.initialize(role));
    }

    /**
     * 同步信息
     */
    public void noticeUpdate() {
        role.sendNow(new OnceFundInfoResp(this));
    }

    /**
     * 领取一次性基金普通奖励
     *
     * @param itemTid 奖励项ID
     * @return 领取结果 无携带数据
     */
    public EcResult<Void> receiveNormalFundReward(int itemTid) {
        EcResult<Void> r = new EcResult<>();
        OnceFundItemRow configRow = role.getGame().table.tradeMall.once_fund_item.get(itemTid);
        if (configRow == null) {
            return r.fail("奖励不存在");
        }
        OnceFundPO onceFundPO = getOnceFundPo(configRow);
        if (onceFundPO == null) {
            return r.fail("奖励不存在");
        }
        return onceFundPO.receiveNormalReward(itemTid);
    }

    /**
     * 领取一次性基金高级奖励
     *
     * @param itemTid 奖励项ID
     * @return 领取结果 无携带数据
     */
    public EcResult<Void> receiveAdvanceFundReward(int itemTid) {
        EcResult<Void> r = new EcResult<>();
        OnceFundItemRow configRow = role.getGame().table.tradeMall.once_fund_item.get(itemTid);
        if (configRow == null) {
            return r.fail("奖励不存在");
        }
        OnceFundPO onceFundPO = getOnceFundPo(configRow);
        if (onceFundPO == null) {
            return r.fail("奖励不存在");
        }
        return onceFundPO.receiveAdvanceReward(itemTid);
    }

   private OnceFundPO getOnceFundPo(OnceFundItemRow configRow) {
       int type = configRow.fundType;
       int fundGroup = configRow.fundGroup;
        for (OnceFundPO onceFundPO : fundMap.values()) {
            if (onceFundPO.type == type && onceFundPO.fundGroup == fundGroup) {
                return onceFundPO;
            }
        }
        return null;
   }


    /**
     * 一键领取基金奖励
     *
     * @param tid 基金模板ID
     * @return 领取结果 无携带数据
     */
    public EcResult<Void> oneClickReceiveFundReward(int tid) {
        EcResult<Void> r = new EcResult<>();
        OnceFundPO onceFundPO = fundMap.get(tid);
        if (onceFundPO == null) {
            return r.fail("基金不存在");
        }
        return onceFundPO.oneClickReceiveReward();
    }

    /**
     * 检测是否允许购买一次性基金
     *
     * @param tid 配置ID
     * @return 检测结果 无携带数据
     */
    public GameResult<Void> payAdvanceAllow(int tid) {
        GameResult<Void> r = new GameResult<>();
        OnceFundPO onceFundPO = fundMap.get(tid);
        if (onceFundPO == null) {
            return r.fail("基金不存在");
        }
        return onceFundPO.payAdvanceAllow();
    }

    /**
     * 激活基金
     *
     * @param tid 配置ID
     */
    public void activeFund(int tid) {
        EcResult<Void> r = new EcResult<>();
        OnceFundPO onceFundPO = fundMap.get(tid);
        if (onceFundPO == null) {
            return;
        }
        onceFundPO.activeFund();
        noticeUpdate();
    }

    /** 快速作战基金ID */
    public static final int FUND_ID_QUICK_FIGHT = 4;

    /**
     * 快速作战成功
     */
    public void onQuickFight() {
        OnceFundPO onceFundPO = fundMap.get(FUND_ID_QUICK_FIGHT);
        if (onceFundPO != null) {
            onceFundPO.targetValue += 1;
            noticeUpdate();
        }
    }
}
